<?php
// +----------------------------------------------------------------------
// | ThinkPHP5.0 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 郉仲生 <540426628@QQ.com>
// +----------------------------------------------------------------------
// | Date：2017年6月30日
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\Cate as CateModel;
use app\admin\model\Product as ProductModel;
use app\admin\controller\Common;

class Product extends Common
{

    public function lst()
    {
        $artres=db('Product')->field('a.*,b.catename')->alias('a')->join('bk_cate b','a.cateid=b.id')->order('a.id desc')->paginate(2);
        $this->assign('artres',$artres);
        return view();
    }

    public function add()
    {
        if(request()->isPost())
        {
            $data=input('post.');
            $data['time']=time();
            $data['addtime']=time();
            $data['updatetime']=time();
            $validate = \think\Loader::validate('Product');
            if(!$validate->scene('add')->check($data))
            {
                $this->error($validate->getError());
            }
            $Product=new ProductModel;
            if($Product->save($data))
            {
                $this->success('添加产品成功',url('lst'));
            }else{
                $this->error('添加产品失败！');
            }
            return;
        }
        $cate=new CateModel();
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);
        return view();
    }

    public function edit()
    {
        if(request()->isPost())
        {
            $data=input('post.');
            $data['updatetime']=time();
            $validate = \think\Loader::validate('Product');
            if(!$validate->scene('edit')->check($data))
            {
                $this->error($validate->getError());
            }
            $Product=new ProductModel;
            $save=$Product->update($data);
            if($save)
            {
                $this->success('修改产品成功！',url('lst'));
            }else{
                $this->error('修改产品失败！');
            }
            return;
        }
        $cate=new CateModel();
        $cateres=$cate->catetree();
        $arts=db('Product')->where(array('id'=>input('id')))->find();
        $this->assign(array(
            'cateres'=>$cateres,
            'arts'=>$arts,
            ));
        return view();
    }

    public function del()
    {
        if(ProductModel::destroy(input('id')))
        {
            $this->success('删除产品成功！',url('lst'));
        }else{
            $this->error('删除产品失败！');
        }
    }

}
